﻿//TODO: test, SegmentateByUsingEdgeCloneBitmap

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace LowLevelGraphics.Filter
{
    //TODO: speed this up is highly recommended
    /// <summary>
    /// 
    /// </summary>
    public class SegmentateByUsingEdgeCloneBitmap : BaseImageFilter
    {
        protected IFilter m_EdgeFilter = new EmbossLaplacian();

        /// <summary>
        /// Default constructor
        /// </summary>
        public SegmentateByUsingEdgeCloneBitmap()
        {
        }

        /// <summary>
        /// used by cloning
        /// </summary>
        /// <param name="_segmentateByUsingEdgeCloneBitmap"></param>
        internal SegmentateByUsingEdgeCloneBitmap(SegmentateByUsingEdgeCloneBitmap _segmentateByUsingEdgeCloneBitmap)
        {

        }

        // TODO: Check which Edge Filter to use.
        /// <summary>
        /// This filter will first Copy the actual bitmap, then run
        /// an edge filter at it.
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //m_EdgeFilter.Execute(_bitmap);

            UnsafeBitmap _bmEdge = (UnsafeBitmap)_bitmap.Clone();
            //new EmbossLaplacian().Execute(_bmEdge);

            UnsafeBitmap bmEdge = new UnsafeBitmap(_bmEdge);

            Graphics g = Graphics.FromImage(_bitmap.InternalBitmap);
            List<Color> aColor = new List<Color>();

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            Color colorEdge = Color.Transparent;
            Color colorEdgeNext = Color.Transparent;
            Color color = Color.Transparent;
            Color colorAverage = Color.Transparent;

            double nDistance = -1;

            int x = 0;
            int y = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth - 1; x++)
                {
                    colorEdge = bmEdge.GetPixel(x, y);
                    colorEdgeNext = bmEdge.GetPixel(x + 1, y);
                    color = _bitmap.GetPixel(x, y);
                    aColor.Add(color);
                    nDistance = ColorFunctions.Distance2(colorEdge, colorEdgeNext);
                    if (nDistance > 150
                        || ColorFunctions.IsInRangeInclusive(colorEdge, 0, 0, 0, 40, 40, 40)
                        || ColorFunctions.IsInRangeInclusive(colorEdge, 200, 200, 200, 255, 255, 255)
                        )
                    {
                        bmEdge.SetPixel(x, y, Color.Red);
                        colorAverage = Color.Transparent;
                        if (aColor.Count > 1)
                        {
                            colorAverage = ColorFunctions.AverageColor(aColor);
                        }
                        //TODO: Same here, get rid of Unlock/Lock to speed it up even more.
                        _bitmap.UnlockImage();
                        g.DrawLine(new Pen(new SolidBrush(colorAverage)), x - aColor.Count, y, x, y);
                        _bitmap.LockImage();
                        aColor = new List<Color>();
                    }
                }
            }

            _bitmap.Dispose();
            //bmEdge.Dispose();
            return bmEdge;
            //return _bitmap;
        }

        /// <summary>
        /// Use this to clone the filter
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SegmentateByUsingEdgeCloneBitmap(this);
        }
    }
}
